import debounce from './debounce.js';
import throttle from './throttle.js';
import Http from './http.js';
const http = new Http();
const app = getApp();

//点赞
const giveLikeApi = debounce(function (
    type,
    id,
    praiseflg,
    uid = '',
    content = ''
) {
    //type 点赞类型 00=城市讨论点赞、01=ft测试点讨论点赞、02=ft测试路线讨论点赞、03=nfc测试点讨论点赞、04=nfc测试路线讨论点赞、05=营业厅留言点赞、06=租车留言点赞、07=住宿留言点赞、08=通告点赞、09=通告讨论点赞、10=问答点赞、11=问答回答点赞、12=经验点赞、13=日记点赞  14=ft测试点讨论点赞、15=ft测试路线讨论点赞、16=nfc测试点讨论点赞、17=nfc测试路线讨论点赞 18=经验讨论点赞 19~22=车食住行    23~26=车食住行讨论点赞  27 打卡点赞  28打卡评论点赞 29城市公告点赞 30城市公告评论点赞  31营业厅点赞  32营业厅评论点赞  33有问必答点赞
    //praiseflg 点赞状态
    //uid 需要通知的人的uid
    //content 点赞贴的内容/标题
    let data = {
        eventid: id,
        delflg: praiseflg, //01--点赞    00--取消点赞
        type: type,
        notifyuid: uid,
        origincontent: content,
    };
    http.postRequest(
        `cjMap/doPraise?authId=${app.globalData.authId}`,
        data
    ).then((res) => {
        if (res.status == 0) {
        }
    });
},
200);

//评论
const submitComment = throttle(
    function (
        tocommentid,
        eventid,
        type,
        level,
        commentcontent,
        anonymous = '00',
        privateflg = '00',
        touid,
        toreplyid = null
    ) {
        return new Promise((resolve) => {
            //type 点赞类型   01=ft测试点评论、02=ft测试路线评论、03=nfc测试点评论、04=nfc测试路线评论、09=通告评论、10=问答点赞、12=经验评论、13=日记评论 14=营业厅详情评论 19~22=车食住行 27打卡评论 29城市公告评论 31营业厅评论
            //praiseflg 点赞状态
            let data = {
                tocommentid: tocommentid, //被回复的评论id,回复时传
                eventid: eventid, //评论事件id
                type: type,
                level: level, //00评论 01回复
                commentcontent: commentcontent,
                anonymousflg: anonymous,
                privateflg,
                touid,
                toreplyid,
            };
            http.postRequest(
                `cjMap/doComment?authId=${app.globalData.authId}`,
                data
            ).then((res) => {
                if (res.status == 0) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                        duration: 1500,
                    });
                    resolve(res);
                }
            });
        });
    },
    1000,
    {
        trailing: false,
    }
);

// 关注
const concernUser = debounce(function (uid, flg, cb) {
    //flg "00"取消关注 "01"关注
    http.getRequest(
        `cjMap/doConcern?authId=${app.globalData.authId}&concernuid=${uid}&flg=${flg}`
    ).then((res) => {
        if (res.status == 0) {
            cb();
        }
    });
}, 300);

// 订阅
const subscribe = debounce(function (teamid, flg, cb) {
    //flg "00"取消订阅 "01"订阅
    let data = {
        pjid: teamid, //项目组id
        delflg: flg,
    };
    http.postRequest(
        `cjMap/doSubscribe?authId=${app.globalData.authId}`,
        data
    ).then((res) => {
        if (res.status == 0) {
            cb();
        }
    });
}, 300);

/** @param {Array} permissionList 权限数组
 *  @param {String} createUid 创建人，默认是空
 *  @returns {boolean}
 */
const hasPermission = (permissionList, createUid = '') => {
    return (
        permissionList.includes(app.globalData.authority) ||
        app.globalData.uid === createUid
    );
};

module.exports = {
    giveLikeApi,
    submitComment,
    concernUser,
    hasPermission,
    subscribe,
};
